<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\DrugService;
use Illuminate\Http\Request;

class DrugController extends BaseController
{
    //药物

    public function __construct(DrugService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['kword'] = $request->post('keyword', '');
        $res = $this->db->getList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 配送时间列表
     */
    public function orderOption()
    {
        $arys = ['today' => [], 'tomorrow' => []];
        //今天当前-20点
        $dqtime = strtotime(date('Y-m-d H', time()));
        $dqtime = time() > $dqtime ? date('H', time()) + 1 : date('H', time());
        for ($i = $dqtime; $i <= 24; $i ++) {
            $qian = strlen($i)==1 ? '0'.$i : $i;
            $hou = strlen($i+1)==1 ? '0'.($i+1) : ($i+1);
            $arys['today'][] = [
                'title' => $qian.':00-'.$hou.':00'
            ];
        }
        //明天8-20点
        for ($i = 8; $i <= 24; $i ++) {
            $qian = strlen($i)==1 ? '0'.$i : $i;
            $hou = strlen($i+1)==1 ? '0'.($i+1) : ($i+1);
            $arys['tomorrow'][] = [
                'title' => $qian.':00-'.$hou.':00',
            ];
        }
        return $this->toJson(0, '', $arys);;
    }

    /**
     * 下单
     */
    public function orderAdd(Request $request)
    {
        $token = $request->post('token', '');
        $pas['drug_id'] = $request->post('drug_id', 0);
        $pas['num'] = $request->post('num', 0);
        $pas['address_id'] = $request->post('address_id', 0);
        $pas['pstime'] = $request->post('pstime', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['drug_id']) {
            return $this->toJson(-2, '药品ID必须的');
        }
        if (!$pas['num']) {
            return $this->toJson(-2, '数量必须的');
        }
        if (!$pas['address_id']) {
            return $this->toJson(-2, '收货地址地址必须的');
        }
        if (!$pas['pstime']) {
            return $this->toJson(-2, '配送时间必须的');
        }
        $res = $this->db->orderAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}